<template>
  <view class="login-container">
    <!-- 顶部装饰区域 -->
    <view class="top-decoration">
      <view class="circle circle-1"></view>
      <view class="circle circle-2"></view>
      <view class="circle circle-3"></view>
    </view>
    
    <!-- Logo区域 -->
    <view class="logo-section">
      <view class="logo-circle">
        <text class="logo-text">E</text>
      </view>
      <text class="app-name">E-Talk</text>
    </view>
    
    <!-- 欢迎文字 -->
    <view class="welcome-section">
      <text class="welcome-title">欢迎回来</text>
      <text class="welcome-subtitle">登录您的账户继续使用</text>
    </view>
    
    <!-- 登录表单 -->
    <view class="form-section">
      <view class="input-container">
        <view class="input-wrapper">
          <text class="input-icon">👤</text>
          <input 
            v-model="loginForm.email" 
            placeholder="邮箱"
            type="email"
            class="input-field"
          />
        </view>
      </view>
      
      <view class="input-container">
        <view class="input-wrapper">
          <text class="input-icon">🔒</text>
          <input 
            v-model="loginForm.password" 
            type="password"
            placeholder="密码"
            class="input-field"
          />
        </view>
      </view>
      
      <view class="forgot-password">
        <text class="forgot-link" @click="forgotPassword">忘记密码？</text>
      </view>
      
      <button class="login-button" @click="handleLogin" :disabled="loading">
        <text class="button-text">{{ loading ? '登录中...' : '登录' }}</text>
      </button>
    </view>
    
    <!-- 底部注册链接 -->
    <view class="bottom-section">
      <view class="divider">
        <view class="divider-line"></view>
        <text class="divider-text">或</text>
        <view class="divider-line"></view>
      </view>
      
      <view class="register-section">
        <text class="register-text">还没有账户？</text>
        <text class="register-link" @click="goToRegister">立即注册</text>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { AuthAPI } from '@/utils/api'
import { AuthUtils } from '@/utils/auth'
import { ValidationUtils } from '@/utils/validation'

const loginForm = ref({
  email: '',
  password: ''
})

const loading = ref(false)

const handleLogin = async () => {
  // 验证邮箱格式
  const emailValidation = ValidationUtils.validateEmail(loginForm.value.email)
  if (!emailValidation.isValid) {
    uni.showToast({
      title: emailValidation.message || '邮箱格式错误',
      icon: 'none'
    })
    return
  }
  
  // 验证密码
  const passwordValidation = ValidationUtils.validatePassword(loginForm.value.password)
  if (!passwordValidation.isValid) {
    uni.showToast({
      title: passwordValidation.message || '密码格式错误',
      icon: 'none'
    })
    return
  }
  
  loading.value = true
  
  try {
    // 调用登录API
    const result = await AuthAPI.login(loginForm.value.email, loginForm.value.password)
    
    // 登录成功，保存用户信息
    const userInfo = result.data
    
    // 使用认证工具保存用户信息
    AuthUtils.saveUserInfo(userInfo.token, {
      userId: userInfo.userId,
      nickName: userInfo.nickName,
      avatar: userInfo.avatar,
      status: userInfo.status
    })
    
    uni.showToast({
      title: '登录成功',
      icon: 'success'
    })
    
    // 登录成功后跳转到消息页面
    setTimeout(() => {
      uni.switchTab({
        url: '/pages/message'
      })
    }, 1000)
    
  } catch (error: any) {
    console.error('登录请求失败:', error)
    uni.showToast({
      title: error.message || '登录失败，请重试',
      icon: 'none'
    })
  } finally {
    loading.value = false
  }
}

const goToRegister = () => {
  uni.navigateTo({
    url: '/pages/register'
  })
}

const forgotPassword = () => {
  uni.showToast({
    title: '功能开发中',
    icon: 'none'
  })
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 0 50rpx;
  position: relative;
  overflow: hidden;
}

/* 顶部装饰圆圈 */
.top-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 300rpx;
  overflow: hidden;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.circle-1 {
  width: 200rpx;
  height: 200rpx;
  top: -100rpx;
  right: -50rpx;
  animation: float 6s ease-in-out infinite;
}

.circle-2 {
  width: 150rpx;
  height: 150rpx;
  top: 50rpx;
  left: -75rpx;
  animation: float 8s ease-in-out infinite reverse;
}

.circle-3 {
  width: 100rpx;
  height: 100rpx;
  top: 150rpx;
  right: 100rpx;
  animation: float 10s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-20rpx); }
}

/* Logo区域 */
.logo-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 150rpx;
  margin-bottom: 60rpx;
}

.logo-circle {
  width: 120rpx;
  height: 120rpx;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20rpx;
  backdrop-filter: blur(10px);
  border: 2rpx solid rgba(255, 255, 255, 0.3);
}

.logo-text {
  font-size: 48rpx;
  font-weight: bold;
  color: #ffffff;
}

.app-name {
  font-size: 36rpx;
  font-weight: 600;
  color: #ffffff;
  letter-spacing: 2rpx;
}

/* 欢迎区域 */
.welcome-section {
  text-align: center;
  margin-bottom: 80rpx;
}

.welcome-title {
  font-size: 42rpx;
  font-weight: bold;
  color: #ffffff;
  display: block;
  margin-bottom: 16rpx;
}

.welcome-subtitle {
  font-size: 26rpx;
  color: rgba(255, 255, 255, 0.8);
  display: block;
}

/* 表单区域 */
.form-section {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 24rpx;
  padding: 60rpx 40rpx;
  backdrop-filter: blur(20px);
  box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.1);
  border: 1rpx solid rgba(255, 255, 255, 0.2);
}

.input-container {
  margin-bottom: 32rpx;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  background: #f8f9fa;
  border-radius: 16rpx;
  border: 2rpx solid transparent;
  transition: all 0.3s ease;
}

.input-wrapper:focus-within {
  border-color: #667eea;
  background: #ffffff;
  box-shadow: 0 0 0 6rpx rgba(102, 126, 234, 0.1);
}

.input-icon {
  padding: 0 20rpx;
  font-size: 32rpx;
  color: #999;
}

.input-field {
  flex: 1;
  height: 88rpx;
  padding: 0 20rpx 0 0;
  border: none;
  background: transparent;
  font-size: 28rpx;
  color: #333;
  outline: none;
}

.input-field::placeholder {
  color: #999;
}

.forgot-password {
  text-align: right;
  margin-bottom: 40rpx;
}

.forgot-link {
  font-size: 26rpx;
  color: #667eea;
  text-decoration: none;
}

.login-button {
  width: 100%;
  height: 96rpx;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8rpx 24rpx rgba(102, 126, 234, 0.3);
  transition: all 0.3s ease;
}

.login-button:active {
  transform: translateY(2rpx);
  box-shadow: 0 4rpx 12rpx rgba(102, 126, 234, 0.3);
}

.login-button:disabled {
  opacity: 0.6;
  transform: none;
}

.button-text {
  font-size: 32rpx;
  font-weight: 600;
  color: #ffffff;
}

/* 底部区域 */
.bottom-section {
  margin-top: 60rpx;
  padding-bottom: 60rpx;
}

.divider {
  display: flex;
  align-items: center;
  margin-bottom: 40rpx;
}

.divider-line {
  flex: 1;
  height: 1rpx;
  background: rgba(255, 255, 255, 0.3);
}

.divider-text {
  padding: 0 30rpx;
  font-size: 24rpx;
  color: rgba(255, 255, 255, 0.7);
}

.register-section {
  text-align: center;
}

.register-text {
  font-size: 26rpx;
  color: rgba(255, 255, 255, 0.8);
}

.register-link {
  font-size: 26rpx;
  color: #ffffff;
  font-weight: 600;
  margin-left: 10rpx;
  text-decoration: underline;
}
</style>